package com.linlang.grasharepro.utils;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseOpenHelper {

    public static final String DATABASE_NAME = "ecommerce.db"; //数据库名称

    public static final int DATABASE_VERSION = 1;
    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    /**
     * Constructor
     *
     * @param ctx
     */
    public DataBaseOpenHelper(Context ctx) {
        this.context = ctx;
        this.DBHelper = new DatabaseHelper(this.context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DataBaseConstant.CREATE_TABLE_USER);//创建用户表
            db.execSQL(DataBaseConstant.CREATE_TABLE_SHARE_CONTEN);//分享内容表
            db.execSQL(DataBaseConstant.CREATE_TABLE_SHARE_MESSAGE);//留言表
            db.execSQL(DataBaseConstant.CREATE_TABLE_NEW_MESSAGE);//消息表
            db.execSQL(DataBaseConstant.CREATE_TABLE_COLLECT);//收藏表
            db.execSQL(DataBaseConstant.CREATE_TABLE_MY_SHARE);//分享表
            db.execSQL(DataBaseConstant.CREATE_TABLE_FOLLOW);//关注表
            db.execSQL(DataBaseConstant.CREATE_TABLE_FANS);//粉丝
            db.execSQL(DataBaseConstant.CREATE_TABLE_EXPERIENCE_CARD);//经历名片
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,
                              int newVersion) {
            // Adding any table mods to this guy here
        }
    }

    /**
     * open the db
     *
     * @return this
     * @throws SQLException return type: DBAdapter
     */
    public DataBaseOpenHelper open() throws SQLException {
        this.db = this.DBHelper.getWritableDatabase();
        return this;
    }

    /**
     * close the db
     * return type: void
     */
    public void close() {
        this.DBHelper.close();
    }
}